package com.hzhk.happyrent.ui.activity.rent;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hzhk.happyrent.Constants;
import com.hzhk.happyrent.R;
import com.hzhk.happyrent.bean.rent.ProductListModel;
import com.hzhk.happyrent.bean.rent.ProductListModel.DataBean.GoodsBean;
import com.hzhk.happyrent.network.API;
import com.hzhk.happyrent.network.HttpManager;
import com.hzhk.happyrent.network.MyStringResponse;
import com.hzhk.happyrent.ui.adapter.rent.ProductListAdapter;
import com.hzhk.happyrent.ui.view.listview.MaiTianListview;
import com.hzhk.happyrent.utils.ListUtils;
import com.tamic.novate.Throwable;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ProductListActivity extends AppCompatActivity implements  MaiTianListview.OnMaiTianRefreshListener{

    @BindView(R.id.activity_titlebar_left)
    ImageView activityTitlebarLeft;
    @BindView(R.id.activity_titlebar_center)
    TextView activityTitlebarCenter;
    @BindView(R.id.activity_titlebar_right)
    ImageView activityTitlebarRight;
    @BindView(R.id.lv_product)
    MaiTianListview lvProduct;
    private ProductListAdapter productListAdapter;
    private List<GoodsBean> goods=new ArrayList<>();
    private int page=1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_list);
        ButterKnife.bind(this);
        initView();
//        if("2".equals(getIntent().getStringExtra(Constants.GOODS_TYPE))){
//            lvProduct.setVisibility(View.GONE);
//        }else{
            initData();
//        }
    }

    private void initData() {
        Map<String,Object> map=new HashMap<>();
        if(!TextUtils.isEmpty(getIntent().getStringExtra(Constants.ACTION))){
            map.put(Constants.ACTION,getIntent().getStringExtra(Constants.ACTION));
        }
        if(!TextUtils.isEmpty(getIntent().getStringExtra(Constants.SORT_ID))){
            map.put(Constants.SORT_ID,getIntent().getStringExtra(Constants.SORT_ID));
        }
        if(!TextUtils.isEmpty(getIntent().getStringExtra(Constants.GOODS_TYPE))){
            map.put(Constants.GOODS_TYPE,getIntent().getStringExtra(Constants.GOODS_TYPE));
        }
        map.put(Constants.PAGE,page);
        map.put(Constants.LIMIT,Constants.PAGE_SIZE);
        HttpManager.getInstance().get(API.GOODS_LIST, map, new MyStringResponse(this) {
            @Override
            public void onError(Object tag, Throwable e) {
//                Toast.makeText(ProductListActivity.this,e.getMessage(),Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCompleted(Object tag) {
                super.onCompleted(tag);
                lvProduct.stop();
            }

            @Override
            public void onCancel(Object tag, Throwable e) {

            }
            @Override
            public void onNext(Object tag, String response) {
                ProductListModel productListModel = new Gson().fromJson(response,ProductListModel.class);
                if(page==1){
                    goods.clear();
                    goods.addAll(productListModel.getData().getGoods());
                    productListAdapter.setList(goods);
                }else{
                    if(!ListUtils.isEmpty(productListModel.getData().getGoods())){
                        goods.addAll(productListModel.getData().getGoods());
                        productListAdapter.setList(goods);
                        if(productListModel.getData().getGoods().size()<Constants.PAGE_SIZE){
                            lvProduct.allLoadFinish();
                        }
                    }else{
                        lvProduct.allLoadFinish();
                    }
                }
            }
        });
    }

    private void initView() {
        activityTitlebarLeft.setImageResource(R.drawable.back);
        if(!TextUtils.isEmpty(getIntent().getStringExtra("sort_tit"))){
            activityTitlebarCenter.setText(getIntent().getStringExtra("sort_tit"));
        }else{
            activityTitlebarCenter.setText(getString(R.string.product_all));
        }
        activityTitlebarRight.setVisibility(View.GONE);
        lvProduct.setOnMaiTianRefreshListener(this);
        productListAdapter = new ProductListAdapter(this);
        lvProduct.setAdapter(productListAdapter);
        lvProduct.setFocusable(false);
        lvProduct.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            }
        });
    }

    @OnClick(R.id.activity_titlebar_left)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onRefresh() {
        lvProduct.setFooterText(getString(R.string.xlistview_footer_hint_normal));
        lvProduct.setFooterEnable(true);
        page=1;
        initData();
    }

    @Override
    public void onLoadMore() {
        page++;
        initData();
    }
}
